//
//  inputAccessoryView.m
//  InputAccessoryView-WindowLayer
//
//  Created by shake on 14/11/14.
//  Copyright (c) 2014年 uyiuyao. All rights reserved.
//

#import "UUInputAccessoryView.h"

#define UUIAV_MAIN_W    CGRectGetWidth([UIScreen mainScreen].bounds)
#define UUIAV_MAIN_H    CGRectGetHeight([UIScreen mainScreen].bounds)
#define UUIAV_Edge_Hori 15
#define UUIAV_Edge_Vert 7
#define UUIAV_Btn_W    40
#define UUIAV_Btn_H    44


@interface UUInputAccessoryView ()<UITextViewDelegate>
{
    UUInputAccessoryBlock inputBlock;

    UIButton *btnBack;
    UITextView *inputView;
    UITextField *assistView;
    UIButton *BtnSave;
    UILabel *placeholderLabel;
    // dirty code for iOS9
    BOOL shouldDismiss;
}
@end

@implementation UUInputAccessoryView

+ (UUInputAccessoryView*)sharedView {
    static dispatch_once_t once;
    static UUInputAccessoryView *sharedView;
    dispatch_once(&once, ^ {
        sharedView = [[UUInputAccessoryView alloc] init];
        
        UIButton *backgroundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backgroundBtn.frame = CGRectMake(0, 0, UUIAV_MAIN_W, UUIAV_MAIN_H);
        [backgroundBtn addTarget:sharedView action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        
        UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, UUIAV_MAIN_W, 60)];
        toolbar.backgroundColor = [UIColor whiteColor];
        [toolbar layoutIfNeeded];
        
        UIView *textBaseView = [[UIView alloc] initWithFrame:CGRectMake(15, 8.5, UUIAV_MAIN_W - 15 - 91, 44)];
        textBaseView.backgroundColor = UIColorFromRGB(0xF2F2F2);
        textBaseView.layer.cornerRadius = 6;
        [toolbar addSubview:textBaseView];
        
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 15, UUIAV_MAIN_W - 15 - 91, 30)];
        textView.returnKeyType = UIReturnKeyDone;
        textView.enablesReturnKeyAutomatically = YES;
        textView.delegate = sharedView;
        textView.font = [UIFont systemFontOfSize:15];
        textView.backgroundColor = UIColorFromRGB(0xF2F2F2);
        textView.textColor = UIColorFromRGB(0x000000);
        textView.layer.cornerRadius = 6;
        textView.tintColor = UIColorFromRGB(0x00CA9D);
        [toolbar addSubview:textView];
        
        UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 120, 28)];
        placeholderLabel.textColor = UIColorFromRGB(0xA9A9A9);
        placeholderLabel.font = [UIFont systemFontOfSize:15];
        placeholderLabel.backgroundColor = [UIColor clearColor];
        placeholderLabel.text = @"发表我的评论…";
        [textView addSubview:placeholderLabel];
        
        UITextField *assistTxf = [UITextField new];
        assistTxf.returnKeyType = UIReturnKeyDone;
        assistTxf.enablesReturnKeyAutomatically = YES;
        [backgroundBtn addSubview:assistTxf];
        assistTxf.inputAccessoryView = toolbar;

        UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        saveBtn.frame = CGRectMake(UUIAV_MAIN_W - 15 - 64, 10, 64, 38);
        saveBtn.layer.masksToBounds = YES;
        saveBtn.layer.cornerRadius = 19;
        saveBtn.backgroundColor = UIColorFromRGB(0x00CA9D);
        [saveBtn setTitle:@"发送" forState:UIControlStateNormal];
        [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [saveBtn addTarget:sharedView action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        saveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [toolbar addSubview:saveBtn];

        sharedView->btnBack = backgroundBtn;
        sharedView->inputView = textView;
        sharedView->assistView = assistTxf;
        sharedView->BtnSave = saveBtn;
        sharedView->placeholderLabel = placeholderLabel;
        
    });

    return sharedView;
}

+ (void)showBlock:(UUInputAccessoryBlock _Nullable)block
{
    UUInputConfiger *configer = [UUInputConfiger new];
    [[UUInputAccessoryView sharedView] show:block configer:configer];
}

+ (void)showKeyboardType:(UIKeyboardType)type
                   Block:(UUInputAccessoryBlock _Nullable)block
{
    UUInputConfiger *configer = [UUInputConfiger new];
    configer.keyboardType = type;
    [[UUInputAccessoryView sharedView] show:block configer:configer];
}

+ (void)showKeyboardType:(UIKeyboardType)type
                 content:(NSString * _Nullable)content
                   Block:(UUInputAccessoryBlock _Nullable)block
{
    UUInputConfiger *configer = [UUInputConfiger new];
    configer.keyboardType = type;
    configer.content = content;
    [[UUInputAccessoryView sharedView] show:block configer:configer];
}

+ (void)showKeyboardConfige:(UUInputAccessoryConfige _Nullable)confige
                      block:(UUInputAccessoryBlock _Nullable)block
{
    UUInputConfiger *configer = [UUInputConfiger new];
    !confige?:confige(configer);
    [[UUInputAccessoryView sharedView] show:block configer:configer];
}

- (void)show:(UUInputAccessoryBlock)block configer:(UUInputConfiger *_Nullable)configer
{
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    [window addSubview:btnBack];
    
    shouldDismiss = NO;
    
    inputBlock = block;
    inputView.text = configer.content;
    assistView.text = configer.content;
    inputView.keyboardType = configer.keyboardType;
    assistView.keyboardType = configer.keyboardType;
    btnBack.userInteractionEnabled = configer.backgroundUserInterface;
    btnBack.backgroundColor = configer.backgroundColor ?: [UIColor clearColor];
    [assistView becomeFirstResponder];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardDidShowNotification
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification * _Nonnull note) {
                                                      if (!shouldDismiss) {
                                                          [inputView becomeFirstResponder];
                                                      }
                                                  }];
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification * _Nonnull note) {
                                                      [self dismiss];
                                                  }];

}

- (void)btnClick
{
    [inputView resignFirstResponder];
    !inputBlock ?: inputBlock(inputView.text ?: @"");
    [self dismiss];
}

- (void)dismiss
{
    shouldDismiss = YES;
    [inputView resignFirstResponder];
    [btnBack removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// textView's delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [self btnClick];
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length == 0) {
        placeholderLabel.hidden = NO;
    } else {
        placeholderLabel.hidden = YES;
    }
}

@end



@implementation UUInputConfiger

- (instancetype)init
{
    if (self = [super init]) {
        self.backgroundUserInterface = YES;
    }
    return self;
}

@end
